import request from '../../utils/request'
import { baseUrl } from '../../project.config'

// 添加商品数据
export function requestAddShopList (option) {
    return new Promise(resolve => {
        request.post('/pro/add', option).then(res => {
            resolve(res.data)
        })
    })
}

// 修改商品信息

export function requestModifyShopList (proid, data) {
    return new Promise((resolve, reject) => {
        request.put('/pro/modify', {
            proid,
            data
        }).then(res => {
            resolve(res)
        }).catch(err => {
            reject(err)
        })
    })
}

// 删除不需要上传的图片
export function removeUnusedPicture (paths) {
    return new Promise(resolve => {
        request({
            url: baseUrl + '/up/unload',
            method: 'delete',
            data: {
                paths
            }
        }).then(res => {
            resolve(res.data)
        })
    })
}

// 删除在线商品的图片，从数据库移除swiperItem
export function removeUnusedOnlinePicture ({ img, proid }) {
    return new Promise((resolve) => {
        request.post('/pro/removeImg', {
            img,
            proid
        }).then(res => {
            resolve(res.data)
        })
    })
}

// 删除商品
export function requestRemoveShopList (proid) {
    return new Promise(resolve => {
        request.delete('/pro/remove', {
            data: {
                proid
            }
        }).then(res => {
            resolve(res.data)
        })
    })
}

// 请求全部的商品数据
export function requestShopList () {
    return new Promise((resolve => {
        request.get('/pro/list').then(res => {
            resolve(res.data)
        })
    }))
}

// 获取商品详情数据
export function requestShopDetail (proid) {
    return new Promise(resolve => {
        request.get('/pro/detail/' + proid).then(res => {
            resolve(res.data)
        })
    })
}

// 请求分类数据
export function requestCategoryList () {
    return new Promise(resolve => {
        request.get('/pro/getCategory').then(res => {
            resolve(res.data)
        })
    })
}

// 更新商品列表
export function updateShopList (proid, type, flag) {
    return new Promise((resolve => {
        request.post('/pro/updateFlag', {
            proid,
            type,
            flag
        }).then(res => {
            resolve(res.data)
        })
    }))
}

// 获取不同状态下的数据
export function getShopListByState (type) {
    return new Promise(resolve => {
        request.get('/pro/showdata', {
            params: {
                type,
                flag: true
            }
        }).then(res => {
            resolve(res.data)
        })
    })
}
